package shixhun;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

/**
 * JPanel：画布----相片
 * 1、画背景
 */
public class StarJpanel extends JPanel implements Runnable {
    static BufferedImage bg = null;//保存背景图片的
    int [] x=new int [500];
    int [] y=new int [500];
    int [] x1=new int [300];
    int [] y1=new int [300];
    int [] z=new int [10];

    public StarJpanel(){
        Random rand=new Random();
        for(int i=0;i<300;i++){
            x[i]=rand.nextInt(1000);
            y[i]=rand.nextInt(1000);
            x1[i]=rand.nextInt(1000);
            y1[i]=rand.nextInt(1000);

        }
        z[2]=200;
        new Thread(this).start();
    }
    //静态资源的文件加载
    static {
        try {
            bg = ImageIO.read(StarJpanel.class.getResource("bg.jpg"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //重写paint
    @Override
    public void paint(Graphics g) {
        super.paint(g);
//        画月亮

        //画背景
        g.drawImage(bg, 0, 0, 1024, 700, null);
        g.setColor(Color.YELLOW);
        g.fillOval(100,100,100,100);
        g.setColor(Color.BLACK);
        g.fillOval(z[2],100,100,100);
        g.setColor(Color.WHITE);

        for (int i=0;i<300;i++){
            g.drawString("基尼泰梅",x[i],y[i]);
            g.drawString("*",x1[i],y1[i]);
        }
        g.setColor(Color.BLACK);
    }
    public void run(){
        System.out.println(Thread.currentThread().getName());
        while (true){
            for (int i=0;i<y.length;i++){
                   x[i]+=2;
                if(x[i]>=1024){
                    x[i]=0;}
                }
                for (int m=0;m<x1.length;m++){
                    y1[m]+=2;
                    if(y1[m]>=700){
                        y1[m]=0;
                    }
            }

            if(z[2]>100){
                z[2]-=3;
            }else{
                z[2]=200;
            }
            repaint();
            try{
                Thread.sleep(50);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        }
    }
}